K.define('kuser.mapapiment',{
   kpartname:'定时任务信息',//模块名称
   gridid:"kuser_gridlist",//表格列表id
   addwin:"kuser_addwin",//添加弹出win id
   updwim:"kuser_updatewin",//修改弹出win id
   editFlag:undefined,
   /**
    * 初始化
    * @param me
    * @param type
    */
   init: function (me,type) {
		me.kuser_gridlist(me);
		me.showChange(me);
	},
	/**
	 * 渲染表格
	 * @param me
	 */
   kuser_gridlist:function(me){
	   var listid=me.gridid;
	   $("#"+listid,me.contentTarget).datagrid({
		      url:"./mapapi/searchapilist",
		      queryParams:{},
		      title:'',
		      width:'98%',
		      rownumbers: true, //是否显示列数 
		      loadMsg: "正在加载数据...",
		      pagination:true ,
		      //pagePosition:"top",
		      fit: true,   //自适应大小
			  border:false,
			  nowrap: true,//数据长度超出列宽时将会自动截取。
			  fitColumns:true,//自动使列适应表格宽度以防止出现水平滚动。
			  singleSelect : true,
			  autoRowHeight:true,  
			  toolbar: [{
			  	 text:'添加',
				 iconCls: 'icon-add',
				 handler: function(){
					 me.kuser_add(me);
					 }
				},'-',{
			  	 text:'修改',
				 iconCls: 'icon-edit',
				 handler: function(){
					 me.kuser_kupiswin(me);
					}
				},'-',{
				  	 text:'删除',
					 iconCls: 'icon-remove',
					 handler: function(){
						 me.kuser_deletedata(me);
						}
					},{
						text: '任务状态<input type="checkbox" name="ischeck" id="ischeck"  value="NORMAL"/>' 
					}],
				 columns:[[
		         /* { field:'ck',checkbox:true },*/
		           {
		                field : 'id',
		                title : '连接地址',
		                width : 120
		            },{
		                field : 'serviceurl',
		                title : '服务地址',
		                width : 120,
		            }, {
		                field : 'servicemethod',
		                title : '方法名称',
		                width : 80
		            }, {
		                field : 'serviceip',
		                title : '服务ip端口',
		                width : 80
		            }, {
		            	field : 'status',
		                title : '任务状态',
		                width : 60,
		                formatter :function(value, row, index){
		                    if (!value) {
		                        return '';
		                    }
		                    if (value == '1') {
		                        return '有效';
		                    } else if (value == '0') {
		                        return '无效';
		                    }
		                }
		            }, {
		                field : 'ctime',
		                title : '创建时间',
		                width : 80,
		                formatter :  function(value, row, index) {
		                    if (value == null || value == '') {
		                        return '';
		                    }
		                    var dt;
		                    if (value instanceof Date) {
		                        dt = value;
		                    } else {
		                        dt = new Date(value);
		                    }
		                    return dt.format("yyyy-MM-dd hh:mm:ss");
		                }
		            }, {
		                field : 'fhznopersistentfields',
		                title : '备注信息',
		                width : 80
		            }
		            
		      ]],
		       onLoadSuccess:function(data){  
		        $("#"+listid,me.contentTarget).datagrid('fixRowHeight');  
		    },
		    onDblClickRow :function(rowIndex,rowData){
		    	if(rowData){
		    		 me.kuser_update(me,rowData);
		           }
		    	}
		  }); 
		  var p = $("#"+listid,me.contentTarget).datagrid("getPager");  
		  $(p).pagination({  
		       pageSize: 10,//每页显示的记录条数，默认为10 
		       pageList: [10,30, 50, 100],//可以设置每页记录条数的列表  
		       beforePageText: '第',//页数文本框前显示的汉字  
		       afterPageText: '页    共 {pages} 页',  
		       displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
		   }); 
	  },
	  /**
	   * 查询
	   * @param me
	   * @param formid
	   */
	  selectkuser:function(me,formid){
	     var listid=me.gridid;
	     var jhdx=$("#jhdx",'#'+formid,me.contentTarget).val();//交换对象
	     var sjly=$("#sjly",'#'+formid,me.contentTarget).val();//数据来源
	     var jgzt=$("#jgzt",'#'+formid,me.contentTarget).val();//结果状态
		 var param = {"jhdx" :jhdx,"sjly":sjly,"jgzt":jgzt};
		 $('#'+listid,me.contentTarget).datagrid("load",param);
	},
	repalceval:function(kval){
		if(kval=="null" || !kval){
			return "";
		}
		return kval;
	},
	/**
	 * 调整状态信息
	 */
	showChange(me){
		var param = {};
		$("#ischeck",me.contentTarget).change(function() {
			if ($("#ischeck",me.contentTarget).is(":checked")) {
				param = {"state" :"NORMAL"};
			}else{
				param = {"state" :""};
			}
		  var listid=me.gridid;
		  $('#'+listid,me.contentTarget).datagrid("load",param);
		});
	},
	/**
	 * 弹出添加界面
	 * @param me
	 */
	kuser_add:function(me){
		 K.tpl({
				tpl: "static/resources/template/kuser/mapapiment_win.html",
				before:function(data){
					var config={"jobClass":"com.kettle.job.KettleJob"};
					return config;
				},
				success: function(html,data){
					var dialogs = $('<div></div>').dialog({
						title: '添加统一对外配置',
						width:'600',
					    height:'320',
					    iconCls: 'icon-edit',
					    content:html,
					    modal: true,
					    //fit: true, 
					    resizable: true,  //控制窗体拉伸
		               /* minimizable: true,//控制最小化
		                maximizable: true, //控制最大化  
*/					    buttons:[{text:'保存',iconCls:"icon-save",handler:function(){
					    	 me.upresult_save(me,dialogs,1);
					    }},{text:'取消',iconCls:"icon-no",handler:function(){
					            	  $(dialogs).dialog('destroy');
					            	  }}
					                ],
					    closed: false,
					    cache: false,
					    onClose : function() {
		                     $(this).dialog('destroy');
		                 },
		                 onOpen:function(){
		                	 me.onwinsize();
		                 },
		                 onResize:function(){
		                	 me.onwinsize();
		                 },
		                 onMove:function(){
		                	 me.onwinsize();
		                 }
					});
				}
			}); 
		 
  },
  kuser_kupiswin:function(me){
		var rows = $("#kuser_gridlist",me.contentTarget).datagrid('getSelections');
		if(rows.length!=1){
			 K.kmessager('提示','选中一条要调整的表单');  
			 return false;
		}
	    me.kuser_update(me,rows[0]);
	
	},
  kuser_deletedata:function(me){
	    var rows = $("#kuser_gridlist",me.contentTarget).datagrid('getSelections');
		if(rows.length<=0){
			 K.kmessager('提示','至少要选择一条要删除的表单');  
			 return false;
		}
	   $.messager.confirm('提醒','确定要删除该记录吗?',function(isok){
		    if (isok){
		    	 $.ajax({
					  type:"post",
					  url:"./quartz/deleteJob",
					  dataType:"json",
					  data:rows[0],
					  success: function(result) {
						  if(result){
							 var sucess=result.sucess;
							 $("#"+me.gridid,me.contentTarget).datagrid('reload');
							 K.kmessager('提示',result.msg);
						  }else{
							  K.kmessager('提示',result.msg);
						  }
					  }
		    });
		   }
		 });
  },
  /**
   * 修改弹出界面
   */
	kuser_update:function(me,rowData){
	 K.tpl({
			tpl: "static/resources/template/kuser/mapapiment_win.html",
			//url:"kmaincountents/showmsgById",
			//data:{"kid":kid},
			before: function(data){
				return rowData;
			},
			success: function(html,data){
				var dialogs = $('<div></div>').dialog({
					title: '修改统一对外配置',
					width:'600',
				    height:'340',
				    iconCls: 'icon-edit',
				    content:html,
				    modal: true,
				    //fit: true, 
				    resizable: true,  //控制窗体拉伸
	               /* minimizable: true,//控制最小化
	                maximizable: true, //控制最大化*/
				    buttons:[
				     {text:'保存',iconCls:"icon-save",handler:function(){
				    	me.upresult_save(me,dialogs,2);
				    }},{text:'取消',iconCls:"icon-no",handler:function(){
	            	    $(dialogs).dialog('destroy');
	            	  }}
	                ],
				    closed: false,
				    cache: false,
				    onOpen:function(){
	                	 me.onwinsize();
	                 },
	                 onResize:function(){
	                	 me.onwinsize();
	                 },
	                 onMove:function(){
	                	 me.onwinsize();
	                 },
				    onClose : function() {
	                     $(this).dialog('destroy');
	                 }
				}); 
			}
		}); 
	},
	onwinsize:function(){
		 $(".window").css("z-index","1001");
         $(".window-shadow").css("z-index","900");
         $(".window-mask").css("z-index","900");
	},
  //保存数据
 upresult_save:function(me,dialogs,ktype){
	  $.messager.progress();
	  var kurl="./quartz/addJob";
	  if(ktype=='2'){
		  kurl="./quartz/editJob";
	  }
	  $('#kuser_myform',dialogs).form('submit', {
	  url: kurl,
	  onSubmit: function(){
	  		var isValid = $(dialogs).form('validate');
	  		if (!isValid){
	  			$.messager.progress('close');	
	  		}
	  		return isValid;	
	  	},
	  	success: function(result){
	  		$.messager.progress('close');
	  		if(result){
	  			 dialogs.dialog('destroy');
				 $("#"+me.gridid,me.contentTarget).datagrid('reload');
				 var datajson=$.parseJSON(result);
				 K.kmessager('提示',datajson.msg);
			  }else{
				  K.kmessager('提示','数据提交失败！');
			  }
	  	  }
	  });
 },
 kuser_stopstartjob:function(me,ktype){
	 var kurl="";
	 var krows=null;
	 var btnname="";
	 if(ktype=="1" || ktype=="2"){
		var rows = $("#kuser_gridlist",me.contentTarget).datagrid('getSelections');
		if(rows.length!=1){
			 K.kmessager('提示','选中一条要调整的表单');  
			 return false;
		}
		krows=rows[0];
		if(ktype=="1"){
			kurl="./quartz/stopJob";
			btnname="暂停";
		}else{
			kurl="./quartz/resumeJob";
			btnname="开启";
		}
	 }else if(ktype=="3"){
		 kurl="./quartz/startSchedule";
	 }else if(ktype=="4"){
		 kurl="./quartz/shutdownSchedule";
	 }else{
		 K.kmessager('提示','出现异常');
		 return false;
	 }
	$.messager.confirm('提醒','确定要执行'+btnname+'吗?',function(isok){
	if (isok){
	 $.ajax({
		  type:"post",
		  url:kurl,
		  dataType:"json",
		  data:krows,
		  success: function(result) {
			  if(result){
				 var sucess=result.sucess;
				 $("#"+me.gridid,me.contentTarget).datagrid('reload');
				 K.kmessager('提示',result.msg);
			  }else{
				  K.kmessager('提示','操作失败！');
			  }
		  }
     });
	}
	});
 },
 intcombox:function(me,dialogs,comboxid,kdatav,kval){
		var datacombox=[]
		$.each(kdatav,function(i,v){
			var config={};
			config.id=v.id;
			config.text=v.text;
			if(v.id==kval){
				config.selected=true;
			}
			datacombox.push(config);
		});
	    $(comboxid,dialogs).combobox({
	        data:datacombox,
	        valueField:'id',
	        textField:'text'
	    });
	}
}, true);